<template>
  <div class="list_container flex both-100">
    <Breadcrumb :title="pageTitle" />
    <div class="content_container">
      <el-form ref="searchMap" class="serach_wrapper flex-space-between" :inline="true" :model="searchMap" @submit.native.prevent>
        <div class="searach">
          <el-form-item prop="keyword" size="small">
            <el-input v-model="searchMap.keyword" placeholder="请输入试卷名称/考试说明" clearable @clear="handleSerach()" @keyup.enter.native="getExamPaperList()" />
          </el-form-item>
          <el-form-item class="flex">
            <el-button @click="handleSerach()" class="btn_primary">查询</el-button>
            <el-button @click="handleReset()" class="btn_cancel">重置</el-button>
          </el-form-item>
        </div>
        <div class="edit flex">
          <!-- <el-button icon="el-icon-plus" class="btn_primary" @click="handleAdd()">
            批量导入
          </el-button> -->
          <el-button icon="el-icon-plus" class="btn_primary" @click="handleAdd()">
            新增
          </el-button>
        </div>
      </el-form>
      <div class="table_container">
        <el-table stripe v-loading="examPaperLoading" :data="examPaperList">
          <el-table-column type="index" label="试卷名称" header-align="center" align="center" width="80"></el-table-column>
          <el-table-column prop="examPaperName" label="试卷名称" header-align="center" align="center" width="80"></el-table-column>
          <el-table-column prop="remark" label="考试说明" align="center"></el-table-column>
          <el-table-column prop="examDuration" label="考试限时（分钟）" align="center"></el-table-column>
          <el-table-column prop="examPassScore" label="通过分数" align="center"></el-table-column>
          <el-table-column label="操作" width="300" align="center">
            <template slot-scope="scope">
              <span type="text" class="tag_primary" size="small" @click="handleDetails(scope.row)">编辑</span>
              <span type="text" class="tag_warning" size="small" @click="handleDelete(scope.row)">删除</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination v-if="total>0" :total="total" :page.sync="searchMap.pageNum" :limit.sync="searchMap.pageSize" @change="examPaperList" />
    </div>
    <PaperDialog ref="paperDialog" @replace="replace"></PaperDialog>
    <el-dialog title="题目选择" class="m_dialog" :visible.sync="dialogQuestionVisable" :close-on-click-modal="false"></el-dialog>
  </div>
</template>
<script>
import { listExamPaper, getExamPaperById, deleteExamPaper } from '@/api/examination'
import PaperDialog from './PaperDialog'

export default {
  data () {
    return {
      // 搜索数据
      searchMap: {
        pageNum: 1, // 当前页
        pageSize: 10,  // 默认条数 
        keyword: undefined,
      },
      // 考试资料数据加载状态
      examPaperLoading: false,
      // 条数
      total: 0,
      // 试卷显示弹框
      dialogVisableExamPaper: false,
      // 标题
      title: '',
      // 考试数据列表数据
      examPaperList: [],
      // 题目选择弹框
      dialogQuestionVisable: false,
    };
  },
  created () {
    this.getExamPaperList();
  },
  computed: {
    pageTitle () {
      return '试卷管理'
    }
  },
  methods: {
    getExamPaperList () {
      this.examPaperLoading = true;
      listExamPaper(this.searchMap).then(res => {
        if (200 === res.code) {
          this.examPaperList = res.rows
          this.total = res.total
          this.examPaperLoading = false
        }
      })
    },
    // 点击详情
    async handleDetails (row) {
      const examPaperId = row.examPaperId
      const response = await getExamPaperById(examPaperId)
      this.$refs.paperDialog.show(response.data)
    },
    // 删除试卷
    handleDelete(row) {
      const examPaperId = [row.examPaperId]
      // this.$confirm('是否确认编号为"' + examPaperId + '"的数据项？').then(res => {
      this.$confirm('是否删除该试卷？').then(res => {
        return deleteExamPaper(examPaperId)
      }).then((res) => {
        if (res.code === 200) {
          this.$message.success('删除成功')
        }
        this.getExamPaperList()
      })
    },
    // 点击新增考试资料
    handleAdd () {
      this.$nextTick(() => {
        console.log(this.$refs.paperDialog)
        this.$refs.paperDialog.show()
      })
      // this.dialogVisableExamPaper = true
    },
    reset () {
      this.formData = {
        examMaterialName: '', // 文件名
        remark: '', // 备注
        examMaterialFiles: [], // 文件列表
      }
    },
    replace () {
      console.log('replace')
      this.getExamPaperList()
    },
    // 查询
    handleSerach () {
      this.getExamPaperList()
    },
    // 重置
    handleReset () {
      this.searchMap = {
        pageNum: 1, // 当前页
        pageSize: 10,  // 默认条数 
        keyword: undefined,
      },
        this.getExamPaperList()
    }
  },
  components: {
    PaperDialog
  }
}
</script>
<style lang='less' scoped>
</style>